/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.Logger;

/**
 *
 * @author PhucAn
 */
public class DBUtil {

    static private Connection conn = null;
    static Logger logger = Logger.getLogger(DBUtil.class);
    static String connectionString = "";
    static String username = "";
    static String password = "";
    static String driver;

    public DBUtil(ServletContext context) {
        connectionString = context.getAttribute(SharingUtil.VAR_A_CONNECTION_URL).toString();
        DBUtil.username = context.getAttribute(SharingUtil.VAR_A_DB_USER).toString();
        DBUtil.password = context.getAttribute(SharingUtil.VAR_A_DB_PASSWORD).toString();
        DBUtil.driver = context.getAttribute(SharingUtil.VAR_A_DB_DRIVER).toString();
    }

    public static Connection getConnection() {
        // readConfig();

        //connectionString = "jdbc:sqlserver://localhost:1433;instanceName=;DatabaseName=online_quiz_db";
        //username = "sa";
        //password = "npan10";
        {

            try {

                //Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
                Class.forName(driver);
                conn = DriverManager.getConnection(connectionString, username, password);

            } catch (SQLException ex) {
                logger.info("Cannot connect to server: " + ex.getMessage());
            } catch (ClassNotFoundException ex) {
                logger.info("Cannot connect to server: " + ex.getMessage());

            }
        }
        return conn;
    }

    public static void disconnect() {
        try {
            conn.close();
        } catch (SQLException ex) {
            logger.info("Error when trying to disconnect: " + ex.getMessage());
        }
    }
//    public static  void readConfig() {
//        if (connectionString.equals("")) {
//            try {
//                String server = "";
//                String port = "";
//                String instanceName = "";
//                String databaseName = "";               
//                Properties props = new Properties();               
//               InputStream ist=getServletContext().getResourceAsStream("/WEB-INF/online_quiz_config.properties");
//                props.load(ist);
//                server = props.getProperty("server", "localhost");
//                port = props.getProperty("port", "1433");
//                instanceName = props.getProperty("instance");
//                databaseName = props.getProperty("database", "QuizShowSystem");
//                username = props.getProperty("user", "sa");
//                password = props.getProperty("password");
//                connectionString="jdbc:sqlserver://"+server+":"+port+";instanceName="+instanceName+";databaseName="+databaseName;
//
//            } catch (FileNotFoundException ex) {
//                logger.info("Input error: " + ex.getMessage());
//            } catch (IOException ex) {
//                logger.info("Error: " + ex.getMessage());
//            }
//        }
//    }
}
